<template>
	<view class="adress">
		
		<div class="adress_box" v-if="JSON.stringify(address) === '{}'">
				<button type="primary" size="mini" class="btnAddress" @click="newaddress">新增收货地址+</button>
		</div>
		
		<div class="rowbox" v-else @click="newaddress">
			<div class="row1">
				<dl>
					<dt>收货人:{{address.userName}}</dt>
					<dt>电话:{{address.telNumber}}</dt>
				</dl>
			</div>
			<div class="row2">
				<dl>
					<dt>收货地址:{{adds}}</dt>
				</dl>
			</div>
		</div>
		
		<img src="../../static/cart_border@2x.png" class="adress_img">
	</view>
</template>

<script>
	import {mapState,mapMutations,mapGetters} from 'vuex'
	export default {
		name:"my-adress",
		data() {
			return {
				
			};
		},
		methods: {
			...mapMutations(['ChangeAddress']),
			async newaddress (state) {
				const [err,suc] = await uni.chooseAddress().catch(err => err)
				if (err === null && suc.errMsg === 'chooseAddress:ok') {
					this.ChangeAddress(suc)
					console.log(suc)
				}
				if (err && (err.errMsg === 'chooseAddress:fail auth deny' || err.errMsg === 'chooseAddress:fail authorize no response')) {
					this.reAuth()
				}
			},
			async reAuth () {
				const [err,confirmResults] = await uni.showModal({
					content: '检测到您没有授权，是否去设置打开',
					confirmText: '确认',
					cancelText: '取消'
				})
				if (err) return
				if (confirmResults.cancel) return uni.$showMsg('您取消了授权')
				if (confirmResults.confirm) {
					return uni.openSetting({
						success: (settingResult) => {
							if (!settingResult.authSetting['scope.address']) return uni.$show('您取消了授权')
							if (settingResult.authSetting['scope.address']) return uni.$showMsg('授权成功，请选择')
						} 
					})
				}
			}
		},
		computed: {
			...mapState(['address']),
			...mapGetters(['adds'])
		}
	}
</script>

<style lang="scss">
.adress {
	.adress_box {
		display: flex;
		align-items: center;
		justify-content: center;
		height: 90px;
	}
	
	.adress_img {
		width: 100%;
		height: 5px;
	}
	.rowbox {
		padding: 0 10px;
		.row1 {
			margin-bottom: 5px;
			dl {
				display: flex;
				align-items: center;
				justify-content: space-between;
			}
		}
		.row2 {
			
		}
	}
}
</style>
